package com.scisoft.scan.ws;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scisoft.scan.data.shelves.ShelvesInfoBean;
import com.scisoft.scan.data.shelves.ShelvesPartInfoBean;
import com.scisoft.scan.data.QueryPageBean;
import com.scisoft.scan.data.stock.StockInfoBean;
import com.scisoft.scan.data.stock.StockInfoPositionBean;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.HeaderProperty;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.List;


public class WebServiceHelper_Shelves {

    public static String GetWS_Url() {
        return String.format("http://%s/dymps/ws/ShelvesService?wsdl", SysParameter.WS_IPandPort);
    }

    public static String WS_Method = "receive";

    public static String WS_Namespace = "http://shelves.scan.ws.dms.com/";


    //查询货架列表
    public static List<ShelvesInfoBean> SHELVES_01(String stockId,String areaId,String bizId, int pagesize, int pageindex) throws Exception {
//        List<ShelvesInfoBean> ret = new ArrayList<>();
//
//        ShelvesInfoBean s1 = new ShelvesInfoBean();
//        s1.depotId = "J01";
//        s1.depotName = "J仓";
//        s1.areaId = "JA01";
//        s1.areaName = "A区";
//        s1.depotId = "JA01-A";
//        s1.shelvesName = "J01-A";
//        ret.add(s1);
//        ShelvesInfoBean s2 = new ShelvesInfoBean();
//        s2.depotId = "J01";
//        s2.depotName = "J仓";
//        s2.areaId = "JB01";
//        s2.areaName = "B区";
//        s2.depotId = "JB01-B";
//        s2.shelvesName = "JB01-B";
//        ret.add(s2);
//        return ret;

        //下面的可以参考，之前的查询条件只有仓库ID，这个项目里面增加了货位ID
        if(bizId==null || "".equals(bizId)){
            bizId="@@";
        }
        if("0".equals(stockId) || "-1".equals(stockId)){
            stockId="@@";
        }
        if("0".equals(areaId)|| "-1".equals(areaId)){
            areaId="@@";
        }
        bizId=bizId+","+stockId+","+areaId;
        List<ShelvesInfoBean> ret = new ArrayList<>();
        String arg0 = String.format("{\"flowNo\":\"SHELVES_01\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\",\"pageIndex\":\"%s\",\"pageSize\":\"%s\"}", SysParameter.user, SysParameter.ScSequence,bizId,pageindex,pagesize);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(), new TypeToken<List<ShelvesInfoBean>>() {}.getType());
        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //创建货架
    public static String SHELVES_02(ShelvesInfoBean shelvesInfoBean) throws Exception {

//        return  "成功";
        //以下接口可以复用
        Gson gson = new Gson();
        String parm = gson.toJson(shelvesInfoBean);
        String arg0 = String.format("{\"flowNo\":\"SHELVES_02\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\"}", SysParameter.user, parm, SysParameter.ScSequence);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {

        } else {
            throw new Exception(msg);
        }
        return msg;
    }

    //编辑货架信息：更新货架号等信息
    public static String SHELVES_03(ShelvesInfoBean shelvesInfoBean) throws Exception {

//        return "成功";
        //一下接口可以复用
        Gson gson = new Gson();
        String parm = gson.toJson(shelvesInfoBean);
        String arg0 = String.format("{\"flowNo\":\"SHELVES_03\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\"}", SysParameter.user, parm, SysParameter.ScSequence);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {

        } else {
            throw new Exception(msg);
        }
        return msg;
    }

    //查询货架上配件列表
    public static List<ShelvesPartInfoBean> SHELVES_04(String shelvesId) throws Exception {
//        List<ShelvesPartInfoBean>  ret = new ArrayList<>();
//        ShelvesPartInfoBean s1 = new ShelvesPartInfoBean();
//        s1.partNo = "P0111";
//        s1.supplierCode = "Dy001";
//        s1.supplierName = "大运";
//        s1.partName = "汽车车窗";
//        ret.add(s1);
//        return ret;

        //以下代码可以复用
        List<ShelvesPartInfoBean> ret = new ArrayList<>();
        String arg0 = String.format("{\"flowNo\":\"SHELVES_04\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence,shelvesId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(), new TypeToken<List<ShelvesPartInfoBean>>() {}.getType());
        } else {
            throw new Exception(msg);
        }
        return ret;

    }

    //查询该仓库下的无货架的配件
    public static List<StockInfoBean> SHELVES_05(String bizId,String depotId,int pageSize,int currentPage)throws Exception {
//        List<StockInfoBean> ret = new ArrayList<>();
//        StockInfoBean s1 = new StockInfoBean();
//        s1.partId = "1";
//        s1.partCode = "P01";
//        s1.supplierCode = "M1";
//        s1.supplierName = "大运";
//        s1.partName = "汽车窗户";
//        s1.depotName="123";
//        s1.position = "234";
//        s1.stockCount="12";
//        s1.canUseCount = "22";
//        ret.add(s1);
//        return ret;

        //以下代码可以复用
        List<StockInfoBean> ret = new ArrayList<>();
        if(!(bizId==null || "".equals(bizId))){
            bizId=bizId+","+depotId;
        }else{
            bizId="@@"+","+depotId;
        }

        String arg0 = String.format( "{\"flowNo\":\"SHELVES_05\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\",\"pageSize\":\"%s\",\"pageIndex\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId,pageSize,currentPage);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<StockInfoBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //配件上架 shelvesId 货架id   partId配件id    supplierCode 供应商代码
    public static String SHELVES_06(String shelvesId,String partId,String supplierCode)throws Exception {

//        return "成功";
        //以下接口可以复用
        ShelvesPartInfoBean bean=new ShelvesPartInfoBean();
        bean.shelvesId=shelvesId;
        bean.partId=partId;
        bean.supplierCode = supplierCode;
        Gson gson = new Gson();
        String parm = gson.toJson(bean);
        String arg0 = String.format("{\"flowNo\":\"SHELVES_06\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\"}", SysParameter.user, parm, SysParameter.ScSequence);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {

        } else {
            throw new Exception(msg);
        }
        return msg;
    }

    //根据仓库ID和过滤条件查询分页情况 stockId 仓库编号 areaId 库区编号 bizId 搜索的查询条件
    public static QueryPageBean SHELVES_07(String stockId,String areaId, String bizId) throws Exception {
//        QueryPageBean ret = new QueryPageBean();
//        ret.pageCount = 3;
//        ret.pageSize = 20;
//        ret.totalCount = 58;
//        return ret;
        //以下代码可以复用
        if(bizId==null || "".equals(bizId)){
            bizId="@@";
        }
        if("0".equals(stockId) || "-1".equals(stockId)){
            stockId="@@";
        }
        if("0".equals(areaId)|| "-1".equals(areaId)){
            areaId="@@";
        }
        bizId=bizId+","+stockId+","+areaId;
        String arg0 = String.format( "{\"flowNo\":\"SHELVES_07\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        QueryPageBean ret = new QueryPageBean();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<QueryPageBean>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //删除货架 shelvesId 货架Id
    public static String SHELVES_08(String shelvesId) throws Exception {

//        return "成功";
        //以下代码可以复用
        Gson gson = new Gson();
        String arg0 = String.format("{\"flowNo\":\"SHELVES_08\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence,shelvesId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret= "成功";
        if (msg != null && msg.contains("成功")) {

        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //配件下架 ，只传库存ID
    public static String SHELVES_09(String relId,String stockId)throws Exception {

//        return "成功";
        //下面接口可以复用
        Gson gson = new Gson();
        if(stockId==null || "".equals(stockId)){
            stockId="@@";
        }
        String bizId=relId+","+stockId;
        String arg0 = String.format("{\"flowNo\":\"SHELVES_09\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret= "成功";
        if (msg != null && msg.contains("成功")) {

        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //修改货架 货架 仓库ID
    public static String SHELVES_10(StockInfoPositionBean bean)throws Exception {

//        return "成功";
        //以下代码可以复用，能否考虑一下，修改货位的时候，同事自动更改库区？没有必要让用户自己来调整库区。
        String stockId=bean.stockId;
        String position=bean.newPosition;
        if(stockId==null || "".equals(stockId)){
            stockId="@@";
        }
        String bizId=position+","+stockId;
        Gson gson = new Gson();
        String arg0 = String.format("{\"flowNo\":\"SHELVES_10\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        String ret= "成功";
        if (msg != null && msg.contains("成功")) {

        } else {
            throw new Exception(msg);
        }
        return ret;
    }

    //配件上架查询配件信息分页
    public static QueryPageBean SHELVES_11(String bizId, String depotId) throws Exception {

//        QueryPageBean ret = new QueryPageBean();
//        ret.pageCount = 3;
//        ret.pageSize = 20;
//        ret.totalCount = 58;
//        return ret;
        //以下代码可以复用
        if(bizId==null || "".equals(bizId)){
            bizId="@@";
        }
        if("0".equals(depotId)){
            depotId="@@";
        }
        bizId= bizId + ","+depotId;
        String arg0 = String.format( "{\"flowNo\":\"SHELVES_11\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,bizId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        QueryPageBean ret = new QueryPageBean();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<QueryPageBean>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;

    }


    public static SoapObject CallWebservice(String arg0) throws Exception {
        //实例化SoapObject对象：命名空间+方法名称
        SoapObject request = new SoapObject(WS_Namespace, WS_Method);
        //设置WebService接口需要传入的参数
        request.addProperty("arg0", arg0);

        //创建SoapSerializationEnvelope 对象，同时指定soap版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = false;//由于是.net开发的webservice，所以这里要设置为true

        //调用接口
        ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
        headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
        HttpTransportSE httpTransportSE = new HttpTransportSE(GetWS_Url());
        httpTransportSE.call(null, envelope, headerPropertyArrayList);//调用

        // 获取返回的数据
        SoapObject result = (SoapObject) envelope.bodyIn;
        return result;
    }
}
